package com.dsg.controller.apply;


import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.apply.DsgApplyCar;
import com.dsg.entity.apply.DsgApplyContact;
import com.dsg.service.apply.IDsgApplyCarService;
import com.dsg.service.apply.IDsgApplyContactService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 連絡人/通訊位址資訊 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-07-10
 */
@RestController
@RequestMapping("/apply/dsgApplyContact")
@Api(description = "連絡人/通訊位址資訊")
public class DsgApplyContactController extends BaseController {
    @Autowired
    IDsgApplyContactService service;
    @Autowired
    IDsgApplyCarService carService;

    @PostMapping(value = "/addBatch")
    @ApiOperation("對單添加聯絡人添加通訊地址")
    public AjaxResult addBatch(@RequestBody List<DsgApplyContact> applyContactList) {
        applyContactList.forEach(e -> {
            if (StringUtil.isEmpty(e.getOid())) {
                e.setOid(UUIDGenerator.getUUID());
            }
        });
        service.saveOrUpdateBatch(applyContactList);
        return success(applyContactList);
    }

    @PutMapping(value = "/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody DsgApplyContact applyContact) {
        boolean flag = service.updateById(applyContact);
        if (flag) {
            return json(MessageConstants.SSO_STATUS_SUCCESS, applyContact);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public AjaxResult delete(String id) {
        Boolean flag = service.removeById(id);
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

//    @GetMapping(value = "/{mainNo}/getByBill")
//    @ApiOperation("查询单条数据")
//    public AjaxResult getByBill(@PathVariable String mainNo) {
//        QueryWrapper wrapper = new QueryWrapper();
//        wrapper.eq("MAIN_NO", mainNo);
//        DsgApplyContact applyContact = service.getOne(wrapper);
//        return json(MessageConstants.SSO_STATUS_SUCCESS, applyContact);
//    }

    @GetMapping(value = "/list")
    @ApiOperation("查询列表")
    public AjaxResult list(DsgApplyContact applyContact) {
        ObjectMapper mapper = new ObjectMapper();
        Map map = mapper.convertValue(applyContact, Map.class);
        List<DsgApplyContact> list = service.listByMap(map);
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, list);
        return ajaxResult;
    }

    @PutMapping(value = "/edit")
    @ApiOperation("重新编辑")
    public AjaxResult edit(@RequestBody List<DsgApplyContact> contactList) {
        for(DsgApplyContact contact : contactList) {
            contact.setVerify(DsgApplyContact.VERIFY_NO);
        }
        service.updateBatchById(contactList);
        return json(MessageConstants.SSO_STATUS_SUCCESS, contactList);
    }

    @PutMapping(value = "/verify")
    @ApiOperation("完成核对")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult verify(@RequestBody List<DsgApplyContact> contactList) {
        for(DsgApplyContact contact : contactList) {
            contact.setVerify(DsgApplyContact.VERIFY_YES);
        }
        service.updateBatchById(contactList);

        //车与通讯资料绑定(有单独的接口设置，这里不需要了)
//        List<DsgApplyContact> addresList = contactList.stream().filter(e -> e.getContactType().equals(DsgApplyContact.CONTACT_TYPE_ADDRESS)).collect(Collectors.toList());
//        for (DsgApplyContact adddress : addresList) {
//            List<String> carOidList = adddress.getCarOidList();
//            if (CollectionUtil.isNotEmpty(carOidList)) {
//                for (String carOid : carOidList) {
//                    DsgApplyCar car = carService.getById(carOid);
//                    car.setCommId(adddress.getOid());
//                    carService.updateById(car);
//                }
//            }
//        }

        return json(MessageConstants.SSO_STATUS_SUCCESS, contactList);
    }
}
